/*
 * reply.c
 *
 */

#include "reply.h"

#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>

void dlmd_reply_ok(int fd)
{
   struct reply * reply_ok = (struct reply*) malloc(sizeof(struct reply));
   reply_ok->code=DLM_reply_OK;
   write(fd, reply_ok, sizeof(struct reply));
}
void dlmd_reply_ne(int fd)
{
   struct reply * reply_ok = (struct reply*)malloc(sizeof(struct reply));
   reply_ok->code=DLM_reply_NOT;
   write(fd, reply_ok, sizeof(struct reply));
}
void dlmd_reply_error(int fd, int code)
{
   struct reply * reply_ok = (struct reply*)malloc(sizeof(struct reply));
   reply_ok->code=code;
   write(fd, reply_ok, sizeof(struct reply));

}
int dlmd_reply_mkfifo(pid_t pid)
{
    //TODO brak ścieżek
    char process_fifo_name[10];
    int fd;
    sprintf(process_fifo_name, "%u", pid);
    if (mkfifo(process_fifo_name, S_IFIFO | S_IRWXU | S_IRWXG | S_IRWXO))
        oops("creating fifo error: %s", strerror(errno));
    if ( (fd = open(process_fifo_name, O_WRONLY)) == -1) {
        err("opening fifo failed: %s", strerror(errno));
        //TODO co się ma stać?
        dlmd_exit(1);
    }
    return fd;
}

int dlmd_reply_resource(int fd, resource_id_t resid)
{
   struct reply * reply_ok = (struct reply*)malloc(sizeof(struct reply));
   reply_ok->code=DLM_reply_OK;
   reply_ok->resource_id=resid;
   write(fd, reply_ok, sizeof(struct reply));
}

